#pragma once
#include <iostream>
#include <cstring>
#include <vector>
#include "Common.hpp"

class Inet_Addr
{
public:
    Inet_Addr(){}
    //从网络转主机
    Inet_Addr(const struct sockaddr_in& addr)
    {
        SetAddr(addr);
    }

    //从主机转网络
    Inet_Addr(const std::string& ip, uint16_t port)
    :_port(port),
     _ip(ip)
    {
        memset(&_addr, 0, sizeof _addr);
        _addr.sin_family = AF_INET;
        _addr.sin_port = htons(port);
        inet_pton(AF_INET, _ip.c_str(), &(_addr.sin_addr));
    }

    void SetAddr(const struct sockaddr_in& addr)
    {
        _addr = addr;
        _port = ntohs(_addr.sin_port);
        char buffer[1024];
        inet_ntop(AF_INET, &_addr.sin_addr, buffer, sizeof buffer);
        _ip = buffer;
    }
    //服务器
    Inet_Addr(uint16_t port)
    {
        memset(&_addr, 0, sizeof _addr);
        _addr.sin_family = AF_INET;
        _addr.sin_port = htons(port);
        _addr.sin_addr.s_addr = INADDR_ANY;
    }
    const struct sockaddr* INetAddr()
    {
        return COVN(_addr);
    }
    int NetAddrLen()
    {
        return sizeof _addr;
    }
    const std::string& IP()
    {
        return _ip;
    }

    const uint16_t& Port()
    {
        return _port;
    }

    const struct sockaddr_in& Sockaddr_In()
    {
        return _addr;
    }
    bool operator==(const Inet_Addr& addr)
    {
        return addr._port == _port && addr._ip == _ip;
    }
    std::string StringAddr()
    {
        return _ip + ":" + std::to_string(_port);
    }
    ~Inet_Addr()
    {

    }
private:
    std::string _ip;
    uint16_t _port;
    struct sockaddr_in _addr;
};